<!--
 * @Description: 
 * @Date: 2024-06-12 08:52:40
 * @LastEditTime: 2024-06-24 10:45:18
-->
<route lang="json5" type="login">
{
  style: {
    navigationBarTitleText: '检察官登录',
  },
}
</route>
<template>
  <view
    class="w-screen h-screen p-t-100rpx px-40rpx flex flex-col box-border overflow-hidden bg-#fff"
  >
    <image class="w-280rpx h-180rpx" :src="leaderLogo" mode="scaleToFill" />
    <view class="text-40rpx font-550">欢迎使用 延安市检察院</view>
    <view class="mt-60rpx mb-100rpx">
      <view class="mb-20rpx border-rd-20rpx overflow-hidden">
        <wd-select-picker
          label="选择区县"
          v-model="userInfo.county"
          :columns="countyList"
          type="radio"
          @change="handleChange"
        ></wd-select-picker>
      </view>
      <view class="mb-20rpx border-rd-20rpx overflow-hidden">
        <wd-input label="账号" v-model="userInfo.accout" placeholder="请输入账号" size="large" />
      </view>
      <view class="mb-20rpx border-rd-20rpx overflow-hidden">
        <wd-input
          label="密码"
          v-model="userInfo.password"
          size="large"
          placeholder="请输入密码"
          show-password
          class="w-100%"
        />
      </view>
    </view>
    <view class="w-100%">
      <wd-button :round="false" type="primary" size="large" block @click="submit">登录</wd-button>
    </view>
  </view>
</template>
<script setup lang="ts">
import { leaderLoginApi, getIdByNames, getTenantIdByName } from '@/service/index/foo'
import { useUserStore } from '@/store'
import { useToast } from 'wot-design-uni'
import leaderLogo from '../../static/login/leaderLoginLogo.png'

const toast = useToast()
const userStore = useUserStore()
const countyList = ref([
  {
    value: '延安市院',
    label: '延安市',
  },
  {
    value: '宝塔区院',
    label: '宝塔区',
  },
  {
    value: '安塞区院',
    label: '安塞区',
  },
  {
    value: '延长县院',
    label: '延长县',
  },
  {
    value: '延川县院',
    label: '延川县',
  },
  {
    value: '子长市县院',
    label: '子长市',
  },
  {
    value: '志丹县院',
    label: '志丹县',
  },
  {
    value: '吴起县院',
    label: '吴起县',
  },
  {
    value: '甘泉县院',
    label: '甘泉县',
  },
  {
    value: '富县院',
    label: '富县',
  },
  {
    value: '洛川县院',
    label: '洛川县',
  },
  {
    value: '宜川县院',
    label: '宜川县',
  },
  {
    value: '黄龙县院',
    label: '黄龙县',
  },
  {
    value: '黄陵县院',
    label: '黄陵县',
  },
])

const userInfo = reactive({
  county: '',
  accout: '',
  password: '',
})
onShow(() => {
  getTenantIdList()
})

// 获取租户列表
function getTenantIdList() {
  getIdByNames().then(async (res) => {
    userInfo.county = userStore.userInfo.tenantId + '' || '167'
    countyList.value.length = 0
    res.data.list.forEach((item) => {
      if (item.packageId != 0) {
        countyList.value.push({
          value: item.id,
          label: item.name,
        })
      }
    })
  })
}

function handleChange({ value }) {
  userStore.setUserInfo({ tenantId: value })
}

async function submit() {
  await leaderLoginApi({
    username: userInfo.accout,
    password: userInfo.password,
    tenantId: userInfo.county,
  }).then((res) => {
    if (!res.data) {
      return toast.error(res.msg)
    }
    userStore.setUserInfo({
      token: res.data.accessToken,
      phone: res.data.phone,
      openid: res.data.openid,
      userId: res.data.userId,
      nickname: res.data.name as string,
      currentRole: 'leader',
    })
    uni.switchTab({ url: '/pages/home/index' })
  })
}
</script>
<style lang="scss" scope>
::v-deep .wd-input {
  background: #f8f8f8 !important;
}

::v-deep .wd-input__clear {
  background: #f8f8f8 !important;
}
</style>
